package com.bj.disc.fragment

import android.annotation.SuppressLint
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import com.ai3d.rfid.R
import com.ai3d.rfid.base.fragment.BaseVmVdbFragment
import com.ai3d.rfid.common.event.EventMgs
import com.ai3d.rfid.common.libs.InitLanguage
import com.ai3d.rfid.common.proxy.MMKVProxy
import com.ai3d.rfid.databinding.FragmentMainNewBinding
import com.bj.disc.ui.main.vm.MainViewModel
import com.bj.disc.database.EventReq
import com.bj.disc.util.DiscConstants
import com.bj.disc.util.DiscConstants.EVENT_TYPE_RESET
import com.bj.disc.util.DiscConstants.GAME_NAME_OC
import com.bj.disc.util.DiscConstants.GAME_NAME_UOT
import com.bj.disc.util.FileConstant
import com.bj.disc.util.GameUotUtil
import com.bj.disc.util.PlayerManager
import com.google.android.exoplayer2.ExoPlaybackException

/**
 * @ProjectName : Kolb
 * @Author : weijinsong
 * @Time : 2023/1/4 16:53
 * @Description : 登录
 *
 * amy在这里是怎么播放的
 * 循环播放双语交替播放Introduction.wav音频吗 你好我叫阿美欢迎来到这个游戏  每隔10S钟
 * 那可不可以点击amy播放不同的声音
 * 我看文件夹里还有其他文件Mainmenu.wav 请选择一个游戏来玩 start1 start2等
 *
 *
 *
 *
 */
class MainFragment : BaseVmVdbFragment<MainViewModel, FragmentMainNewBinding>() {

    val action: Action by lazy { Action() }
    private var playerManager: PlayerManager? = null



    override fun initView(savedInstanceState: Bundle?) {
        // mDatabind.vm = mViewModel
        mDatabind.action = action
        playerManager = context?.let { PlayerManager(it) }
        amyAnimation = mDatabind.ivAmy.drawable as AnimationDrawable
        playerManager?.setCallback(object : PlayerManager.AudioPlayerCallback {
            override fun onPlaybackStarted() {
                mDatabind.ivAmy.setImageDrawable(resources.getDrawable(R.drawable.amy_talk_animation))
                amyAnimation = mDatabind.ivAmy.drawable as AnimationDrawable
                amyAnimation!!.start()
                baseHandler.removeCallbacksAndMessages(null)
            }

            override fun onPlaybackCompleted() {
                amyAnimation!!.selectDrawable(0)
                amyAnimation!!.stop()
                baseHandler.sendEmptyMessageDelayed(0,3*1000)

            }

            override fun onPlaybackError(error: ExoPlaybackException) {
                amyAnimation!!.selectDrawable(0)
                amyAnimation!!.stop()
                baseHandler.sendEmptyMessageDelayed(0,3*1000)
            }
        })
        generateGameSessionId()

    }

    private fun generateGameSessionId() {
        var userId = MMKVProxy.getString(MMKVProxy.KEY_USER_ID)
        //登录用户名+游戏类别作为key 值的话就是currentHard,currentLevel,currentRound,rate
        //是否重置UOT
        if (MMKVProxy.getBoolean(MMKVProxy.KEY_UOT_GAME_RESET, false)) {
            var data = MMKVProxy.getString(MMKVProxy.GAME_PROGRESS + userId + MMKVProxy.UOT, "")
            MMKVProxy.setString(MMKVProxy.UOT_GAMESESSIONID, "")
            MMKVProxy.setString(MMKVProxy.UOT_LEVELSESSIONID, "")
            MMKVProxy.setString(MMKVProxy.UOT_SESSIONID, "")
            if (data.isNotEmpty()) {
                var arr = data.split(",")
                var currentHard = arr[0].toInt()
                MMKVProxy.setString(MMKVProxy.UOT_LEVELSESSIONID + currentHard, "")

            }
            MMKVProxy.setString(MMKVProxy.GAME_PROGRESS + userId + MMKVProxy.UOT, "")
            MMKVProxy.setBoolean(MMKVProxy.KEY_UOT_GAME_RESET, false)
            MMKVProxy.setBoolean(MMKVProxy.UOT_IS_SHOW_FIRST_SECOND_LEVEL, false)
            collectUploadData(GAME_NAME_UOT)
        }
        //是否重置OC
        if (MMKVProxy.getBoolean(MMKVProxy.KEY_OC_GAME_RESET, false)) {
            var data = MMKVProxy.getString(MMKVProxy.GAME_PROGRESS + userId + MMKVProxy.OC, "")
            MMKVProxy.setString(MMKVProxy.OC_GAMESESSIONID, "")
            MMKVProxy.setString(MMKVProxy.OC_LEVELSESSIONID, "")
            MMKVProxy.setString(MMKVProxy.OC_SESSIONID, "")
            if (data.isNotEmpty()) {
                var arr = data.split(",")
                var currentHard = arr[0].toInt()
                MMKVProxy.setString(MMKVProxy.OC_LEVELSESSIONID + currentHard, "")
                MMKVProxy.setString(MMKVProxy.OC_SESSIONID, "")
            }
            MMKVProxy.setString(MMKVProxy.GAME_PROGRESS + userId + MMKVProxy.OC, "")
            MMKVProxy.setBoolean(MMKVProxy.KEY_OC_GAME_RESET, false)
            MMKVProxy.setBoolean(MMKVProxy.OC_IS_SHOW_FIRST_SECOND_LEVEL, false)
            collectUploadData(GAME_NAME_OC)
        }

        //是否重置VF
        if (MMKVProxy.getBoolean(MMKVProxy.KEY_DF_GAME_RESET, false)) {
            var data = MMKVProxy.getString(MMKVProxy.GAME_PROGRESS + userId + MMKVProxy.VF, "")
            MMKVProxy.setString(MMKVProxy.VF_GAMESESSIONID, "")
            MMKVProxy.setString(MMKVProxy.VF_LEVELSESSIONID, "")
            MMKVProxy.setString(MMKVProxy.VF_SESSIONID, "")
            if (data.isNotEmpty()) {
                var arr = data.split(",")
                var currentHard = arr[0].toInt()
                MMKVProxy.setString(MMKVProxy.VF_LEVELSESSIONID + currentHard, "")

            }
            MMKVProxy.setString(MMKVProxy.GAME_PROGRESS + userId + MMKVProxy.VF, "")
            MMKVProxy.setBoolean(MMKVProxy.KEY_DF_GAME_RESET, false)
            collectUploadData(DiscConstants.GAME_NAME_VF)
        }

        if (MMKVProxy.getString(MMKVProxy.UOT_GAMESESSIONID, "").isEmpty()) {
            MMKVProxy.setString(MMKVProxy.UOT_GAMESESSIONID, GameUotUtil.getGameSessionId("uot"))
        }
        if (MMKVProxy.getString(MMKVProxy.OC_GAMESESSIONID, "").isEmpty()) {
            MMKVProxy.setString(MMKVProxy.OC_GAMESESSIONID, GameUotUtil.getGameSessionId("oc"))
        }
        if (MMKVProxy.getString(MMKVProxy.VF_GAMESESSIONID, "").isEmpty()) {
            MMKVProxy.setString(MMKVProxy.VF_GAMESESSIONID, GameUotUtil.getGameSessionId("vf"))
        }

    }


    private fun play() {
        var fileName = ArrayList<String>()
        fileName.add(FileConstant.main_introduce)
        fileName.add(FileConstant.main_mainmenu)
        playerManager?.setLoopCount(Int.MAX_VALUE)
        playerManager?.setLoopInterval(10 * 1000)
        playerManager?.play(FileConstant.main_path, fileName)
    }

    override fun initData() {
        super.initData()

    }

    override fun onPause() {
        super.onPause()
        playerManager?.pauseAudio()
    }

    @SuppressLint("SetTextI18n")
    override fun createObserver() {
        super.createObserver()
    }

    override fun onResume() {
        super.onResume()
        play()
    }

    override fun onDestroy() {
        super.onDestroy()
        playerManager?.release()
        amyAnimation?.stop()
    }

    fun collectUploadData(gameName: Int) {
        var eventReq = EventReq(
            gameName,
            null,
            null,
            null,
            null,
            EVENT_TYPE_RESET,
            null,
            null
        )
        EventMgs.putCache(eventReq)
    }


    inner class Action {
        fun goBack() {
//            requireActivity().finish()
            InitLanguage.finishAll()
        }

        fun intentToGame(gameName: Int) {
           

        }

        fun logout() {
            //LogoutDialog(requireActivity()).show()
        }

        fun gotoUserReport() {
           // UserReportActivity.startUserReportActivity(requireActivity())
        }
    }
}